<?php

// Namespace

namespace Loop;


/**
 * CDatabaseDriverFactory takes care of creating the right database driver
 *
 * @package		Loop
 * @category	Factory
 */

class CDatabaseDriverFactory
{
	/**
	 * Creates a database driver (MySQL, MySQLi, etc. as specified in preferences)
	 * 
	 * @return	IDatabaseDriver	The database driver
	 */
	
	public function createDatabaseDriver($extensionId)
	{
		// Use the right extension ID for shared databases

		if (pref('[' . $extensionId . ']/database/isShared') == true)
		{
			$extensionId = EXTENSION_APPLICATION;
		}


		// Check whether the driver exists
		
		$className = 'C' . pref('[Loop]/database/driver') . 'Driver';
		$pathToDriver = PATH_LOOP . 'Class/' . $className . '.php';

		if (file_exists($pathToDriver) == false)
		{
			e
			(
				EXCEPTION_DATABASE_DRIVER_NOT_FOUND,
				array
				(
					'databaseDriver' => pref('[Loop]/database/driver')
				)
			);
		}


		// Create the driver

		require_once($pathToDriver);


		// Is there a namespace ?
		
		$classObject = 'Loop\\' . $className;

		$databaseDriver = new $classObject($extensionId);
		
		
		return $databaseDriver;
	}
}

?>
